<?php

function modifyLocationState($BDD_connexion,$ownerId,$locationId,$beginTime,$endTime,$state)
{
	
	$request = "SELECT * FROM location WHERE id_location=:id";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$locationId,PDO::PARAM_INT);
	$prepare->execute();
	$located = $prepare->fetch(PDO::FETCH_OBJ);
	
	$locationBeginTime = calculateTimestamp(substr($located->date_debut,0,-3));
	
	//Cette manivelle va nous sortir la date en augmentant le jour de la duree. Si on dépasse les 31, mktime dans calculateTimestamp corrigera sans soucis.
	$locatedexploded = explode(' ',$located->date_debut);
	$locatedexploded[0] = explode('-',$locatedexploded[0]);
	$locatedexploded[0][2] = $locatedexploded[0][2]+$located->duree;
	$locatedexploded[0] = implode("-",$locatedexploded[0]);
	$locationEnd = implode(" ",$locatedexploded);
	
	$locationEndTime = calculateTimestamp(substr($locationEnd,0,-3));
	
	if ($locationBeginTime < $beginTime)
	{
		$numberBefore = numberOfDays($locationBeginTime,$beginTime);
		if ($numberBefore > 0)
		{
			$price = $numberBefore / $located->duree;
			$price *= $located->prix;
			
			$promo = $numberBefore / $located->duree;
			$promo *= $located->promo;
			
			addLocation($BDD_connexion,$locationBeginTime,$numberBefore,intval($price),intval($promo),$located->etat,$located->ref_bien,$located->ref_personne);
		}
	}
	
	changeState($BDD_connexion,$locationId,$state);
	
	updateBeginLocation($BDD_connexion,$locationId,date("Y-m-d H:i:s",$beginTime));
	
	$duration = numberOfDays($beginTime,$endTime);
	updateDurationLocation($BDD_connexion,$locationId,$duration);
	
	$price = intval(($located->prix*$duration)/$located->duree);
	updatePriceLocation($BDD_connexion,$locationId,$price);
	
	$promo = intval(($located->promo*$duration)/$located->duree);
	updatePromoLocation($BDD_connexion,$locationId,$promo);
	
	if ($locationEndTime > $endTime)
	{
		$numberAfter = numberOfDays($endTime,$locationEndTime);
		if ($numberAfter > 0)
		{
			$price = $numberAfter / $located->duree;
			$price *= $located->prix;
			
			$promo = $numberAfter / $located->duree;
			$promo *= $located->promo;
			
			addLocation($BDD_connexion,$endTime,$numberAfter,intval($price),intval($promo),$located->etat,$located->ref_bien,$located->ref_personne);
		}
	}
	
	return true;
	
}

function changeState($BDD_connexion,$id,$newState)
{
	
	$request = "UPDATE location SET etat = :etat WHERE id_location = :id";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->bindValue(':etat',$newState,PDO::PARAM_INT);
	return $prepare->execute();
	
}

function updateBeginLocation($BDD_connexion,$id,$newDate)
{
	
	$request = "UPDATE location SET date_debut = :date WHERE id_location = :id";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->bindParam(':date',$newDate,PDO::PARAM_STR);
	
	return $prepare->execute();
	
}

function updateDurationLocation($BDD_connexion,$id,$newDuration)
{
	
	$request = "UPDATE location SET duree = :duree WHERE id_location = :id";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->bindValue(':duree',$newDuration,PDO::PARAM_INT);
	return $prepare->execute();
	
}

function updatePriceLocation($BDD_connexion,$id,$newPrice)
{
	
	$request = "UPDATE location SET prix = :prix WHERE id_location = :id";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->bindValue(':prix',$newPrice,PDO::PARAM_INT);
	return $prepare->execute();
	
}

function updatePromoLocation($BDD_connexion,$id,$newPromo)
{
	
	$request = "UPDATE location SET promo = :promo WHERE id_location = :id";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->bindValue(':promo',$newPromo,PDO::PARAM_INT);
	return $prepare->execute();
	
}

function numberOfDays($begin,$end) {
	
	$days = ($end - $begin)/ 86400;
    if (intval($end - $begin / 86400) < $days)
    {
		$days++;
	}
	
    return $days;
    
}

function addLocation($BDD_connexion,$beginningTimestamp,$duree,$prix,$promo,$etat,$bien,$personne) {

$request = $BDD_connexion->prepare("
	INSERT INTO location (date_debut,duree,prix,promo,etat,ref_bien,ref_personne)
	VALUES (:debut,:duree,:prix,:promo,:etat,:ref_bien,:ref_personne);");
$request->bindValue(':debut', date("Y-m-d H:i:s", $beginningTimestamp), PDO::PARAM_STR);
$request->bindValue(':duree', $duree, PDO::PARAM_INT);
$request->bindValue(':prix', $prix, PDO::PARAM_INT);
$request->bindValue(':promo', $promo, PDO::PARAM_INT);
$request->bindParam(':etat', $etat, PDO::PARAM_INT, 1);
$request->bindValue(':ref_bien', $bien, PDO::PARAM_INT);
$request->bindValue(':ref_personne', $personne, PDO::PARAM_INT);

return $request->execute();

}

function calculateTimestamp($time_string)
{
	$exploded = explode(" ",$time_string);
	$beginExploded = explode("-",$exploded[0]);
	if (count($beginExploded) != 3)
	{
		return -1;
	}
	else
	{
		$beginHourExploded = explode(":",$exploded[1]);
		if (count($beginHourExploded) != 2)
		{
			return -1;
		}
		else
		{
			return mktime($beginHourExploded[0],$beginHourExploded[1],0,$beginExploded[1],$beginExploded[2],$beginExploded[0]);
		}
	}
}

function isNowBooked($BDD_connexion,$id)
{
	
	$beginTime = mktime(00,00,00,date('m'),date('d'),date('Y'));
	$endTime = mktime(00,00,00,date('m')+1,date('d'),date('Y')) - 1;
	
	$locateds = getDaysLocated($BDD_connexion,$id);
	
	foreach ($locateds as $located)
	{
		
		if ($located->begin >= $beginTime && $located->end <= $endTime && $located->etat > 0)
		{
			return true;
		}
		else if ($located->begin <= $beginTime && $located->end >= $beginTime && $located->etat > 0)
		{
			return true;
		}
		else if ($located->begin >= $beginTime && $located->end >= $beginTime && $located->etat > 0)
		{
			return true;
		}
	}
	
	return false;
	
}

function isFreeBetween($BDD_connexion,$id,$beginTime,$endTime)
{
	
	if ($beginTime < 0 || $endTime < 0)
	{
		return -1;
	}
	
	$locateds = getDaysLocated($BDD_connexion,$id);
	
	foreach ($locateds as $located)
	{
		
		if ($located->begin >= $beginTime && $located->end <= $endTime && $located->etat > 0)
		{
			return 0;
		}
		else if ($located->begin <= $beginTime && $located->end >= $beginTime && $located->etat > 0)
		{
			return 0;
		}
		else if ($located->begin >= $beginTime && $located->end >= $beginTime && $located->etat > 0)
		{
			return 0;
		}
	}
	
	//A ce stade aucune location en cours ou occupée n'est disponible dans l'intervalle
	//On cherche donc si une location libre englobe le tout.
	foreach ($locateds as $located)
	{
		
		if ($located->begin <= $beginTime && $located->end >= $beginTime && $located->etat == 0)
		{
			return $located->id_location;
		}
		
	}
	
	return -2; //-2 signifie que rien n'est trouvé.
	
	
}

function getDaysLocated($BDD_connexion,$id)
{
	
	$locateds = array();
	
	$request_count = "SELECT id_location,date_debut,duree,etat,prix,promo FROM location WHERE ref_bien = :id";
	$prepare=$BDD_connexion->prepare($request_count);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->execute();
	while ($located = $prepare->fetch(PDO::FETCH_OBJ))
	{
		$located->begin = calculateTimestamp($located->date_debut);
		$locatedexploded = explode(' ',$located->date_debut);
		$locatedexplodedDate = explode('-',$locatedexploded[0]);
		$located->begin = mktime(00,00,00,$locatedexplodedDate[1],$locatedexplodedDate[2],$locatedexplodedDate[0]);
		$located->end = mktime(00,00,00,$locatedexplodedDate[1],$locatedexplodedDate[2]+($located->duree-1),$locatedexplodedDate[0]);
		$locateds[] = $located;
	}
	
	return $locateds;
}

function isLocatedDay($timestamp,$locateds)
{
	
	$state = -1;
	
	foreach ($locateds as $located)
	{
		if ($timestamp >= $located->begin && $timestamp <= $located->end)
		{
			return $located;
		}
	}
	
}

?>
